package news

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/admin"
	"gq_volunteer_platform/model/news"
	"time"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	var req struct {
		Page        int    `binding:"required,gte=1,max=10000000"`
		Limit       int    `binding:"required,gte=1,max=10000000"`
		Title       string `binding:"max=255"`
		Content     string `binding:"max=255"`
		CheckStatus string `binding:"oneof='' 待审 驳回 已审"`
		CategoryId  uint   `binding:"max=255"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 查询
	var count int64

	// var list []news.News
	type NewsCategory struct {
		ID   uint
		Name string
	}

	var list []struct {
		news.News
		NewsCategory NewsCategory `gorm:"foreignKey:id;references:category_id"`
	}

	var tx = bean.MysqlDb.Model(&news.News{})

	//
	if req.Title != "" {
		tx = tx.Where("`title` like ?", "%"+req.Title+"%")
	}

	if req.Content != "" {
		tx = tx.Where("`Content` like ?", "%"+req.Content+"%")
	}

	if req.CategoryId != 0 {
		tx = tx.Where("`category_id` = ?", req.CategoryId)
	}

	if req.CheckStatus != "" {
		tx = tx.Where("`check_status` = ?", req.CheckStatus)
	}

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Preload("NewsCategory").Limit(req.Limit).Offset(offset).Order("id DESC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})
}

func MyList(c *gin.Context) {

	var req struct {
		Page        int    `binding:"required,gte=1,max=10000000"`
		Limit       int    `binding:"required,gte=1,max=10000000"`
		Title       string `binding:"max=255"`
		Content     string `binding:"max=255"`
		CheckStatus string `binding:"oneof='' 待审 驳回 已审"`
		CategoryId  uint   `binding:"max=255"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	// 获取登录用户对象
	var adm admin.Admin
	if err := adm.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var offset = (req.Page - 1) * req.Limit

	// 查询
	var count int64

	// var list []news.News
	type NewsCategory struct {
		ID   uint
		Name string
	}

	var list []struct {
		news.News
		NewsCategory NewsCategory `gorm:"foreignKey:id;references:category_id"`
	}

	var tx = bean.MysqlDb.Model(&news.News{})
	tx.Where("`organization_id`=?", adm.OrganizationId)

	//
	if req.Title != "" {
		tx = tx.Where("`title` like ?", "%"+req.Title+"%")
	}

	if req.Content != "" {
		tx = tx.Where("`Content` like ?", "%"+req.Content+"%")
	}

	if req.CategoryId != 0 {
		tx = tx.Where("`category_id` = ?", req.CategoryId)
	}

	if req.CheckStatus != "" {
		tx = tx.Where("`check_status` = ?", req.CheckStatus)
	}

	// 查询总数
	if err := tx.Count(&count).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查询列表
	if err := tx.Preload("NewsCategory").Limit(req.Limit).Offset(offset).Order("id DESC").Find(&list).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.PureJSON(200, gin.H{
		"count": count,
		"list":  list,
	})
}

func Edit(c *gin.Context) {
	var req struct {
		ID         uint   `binding:"max=10000000"`
		CategoryId uint   `binding:"max=10000000"`
		Title      string `binding:"max=255"`
		Thumb      string `binding:"max=255"`
		Desc       string `binding:"max=255"`
		Content    string `binding:"max=20000"`
		// Top         string
		// CheckStatus string
		// CheckTime   string `binding:"datetime"`
		ReleaseTime string `binding:"datetime=2006-01-02"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	// 获取登录用户对象
	var adm admin.Admin
	if err := adm.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 查出现有对象
	var one news.News
	one.ID = req.ID
	one.CategoryId = req.CategoryId
	one.Title = req.Title
	one.Thumb = req.Thumb
	one.Desc = req.Desc
	one.Content = req.Content
	one.OrganizationId = adm.OrganizationId
	// one.CheckStatus = req.CheckStatus
	// one.CheckTime = req.CheckTime
	one.ReleaseTime = req.ReleaseTime

	// 保存对象
	if one.ID == 0 {
		one.Top = "否"
		if err := bean.MysqlDb.Create(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, "数据库错误")
			return
		}
	} else {
		one.CheckStatus = "待审"
		if err := bean.MysqlDb.Updates(&one).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, "数据库错误")
			return
		}
	}

	c.String(200, "ok")
}

func Top(c *gin.Context) {
	var req struct {
		ID  uint   `binding:"max=10000000"`
		Top string `binding:"oneof=是 否"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var one news.News
	one.ID = req.ID
	one.Top = req.Top

	if err := bean.MysqlDb.Updates(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	c.String(200, "ok")
}

func Detail(c *gin.Context) {

	var req struct {
		ID uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindQuery(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var one news.News

	if err := bean.MysqlDb.Model(&news.News{}).Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("文章不存在id:%d", req.ID))
		return
	}

	c.PureJSON(200, one)
}

func Delete(c *gin.Context) {
	var req struct {
		ID uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var one news.News
	if err := bean.MysqlDb.Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("文章不存在id:%d", req.ID))
		return
	}

	if err := bean.MysqlDb.Delete(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	c.String(200, "ok")
}

func Check(c *gin.Context) {
	// 获取登录用户对象
	var adm admin.Admin
	if err := adm.Get(c); err != nil {
		bean.Log.Error(c.FullPath())
		bean.Log.Error(err)
		return
	}

	var req struct {
		ID          uint   `binding:"max=10000000"`
		CheckStatus string `binding:"oneof='' 驳回 已审"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err)
		c.String(400, err.Error())
		return
	}

	var one news.News

	if err := bean.MysqlDb.Model(&news.News{}).Where("id=?", req.ID).First(&one).Error; err != nil {
		bean.Log.Warning(err)
		c.String(400, fmt.Sprintf("文章不存在id:%d", req.ID))
		return
	}

	var two news.News
	two.ID = one.ID
	two.CheckStatus = req.CheckStatus
	two.CheckTime = time.Now().Format(time.DateTime)
	two.CheckAdmin = adm.Name

	if err := bean.MysqlDb.Updates(&two).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	c.String(200, "ok")
}
